package com.xingzhe.pengji.security.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;


import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.xingzhe.pengji.framework.bean.BaseObj;

/**
 * @Title ： 三江学院 毕业设计
 * @Description ：
 * @author ：LuFengLiang
 * @Company: IT行者工作室
 * @Copyright: Copyright (c) 2012-2-24
 * @version ： 1.0
 */
public class User extends BaseObj implements UserDetails {
    
   
	private static final long serialVersionUID = 1L;

	private int id;
	private String name;
	private String email;
	private String account;
	private String password;

	// -1为不是系统管理员，1为系统管理员
	private int isSys;
	// -1为不可用，1为可用
	private int enabled;

	private int accountNonLocked;
	private int accountNonExpired;
	private int credentialsNonExpired;

	private String creator;
	private Date createTime;
	private String updateMan;
	private Date updateTime;
	// -1表示为删除，1表示正常使用
	private int delFlag;

	public User() {
	}

	public User(String name, String email, String account, String password,
			int isSys, int enabled, int accountNonLocked,
			int accountNonExpired, int credentialsNonExpired,
			Collection<GrantedAuthority> auths) {
		this.name=name;
		this.email=email;
		this.account=account;
		this.password=password;
		this.isSys=isSys;
		this.enabled=enabled;
		this.accountNonLocked=accountNonLocked;
		this.accountNonExpired=accountNonExpired;
		this.credentialsNonExpired=credentialsNonExpired;
		this.auths=sortAuthorities(auths);

	}

	private Set<GrantedAuthority> auths;

	public Set<GrantedAuthority> getAuths() {
		return auths;
	}

	public void setAuths(Set<GrantedAuthority> auths) {
		this.auths = auths;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getEnabled() {
		return enabled;
	}

	public void setEnabled(int enabled) {
		this.enabled = enabled;
	}

	public int getIsSys() {
		return isSys;
	}

	public void setIsSys(int isSys) {
		this.isSys = isSys;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAccountNonExpired() {
		return accountNonExpired;
	}

	public void setAccountNonExpired(int accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	public int getAccountNonLocked() {
		return accountNonLocked;
	}

	public void setAccountNonLocked(int accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	public int getCredentialsNonExpired() {
		return credentialsNonExpired;
	}

	public void setCredentialsNonExpired(int credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getUpdateMan() {
		return updateMan;
	}

	public void setUpdateMan(String updateMan) {
		this.updateMan = updateMan;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public int getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(int delFlag) {
		this.delFlag = delFlag;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return getAuths();
	}

	@Override
	public String getUsername() {
		return this.getAccount();
	}

	@Override
	public boolean isAccountNonExpired() {
		return this.getAccountNonExpired() == 1 ? true : false;
	}

	@Override
	public boolean isAccountNonLocked() {
		return this.getAccountNonLocked() == 1 ? true : false;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return this.getCredentialsNonExpired() == 1 ? true : false;
	}

	@Override
	public boolean isEnabled() {
		return this.getEnabled() == 1 ? true : false;
	}

	private static SortedSet<GrantedAuthority> sortAuthorities(
			Collection<GrantedAuthority> authorities) {
		// Ensure array iteration order is predictable (as per
		// UserDetails.getAuthorities() contract and SEC-717)
		SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet<GrantedAuthority>(
				new AuthorityComparator());

		for (GrantedAuthority grantedAuthority : authorities) {
			sortedAuthorities.add(grantedAuthority);
		}

		return sortedAuthorities;
	}

	private static class AuthorityComparator implements
			Comparator<GrantedAuthority>, Serializable {
		private static final long serialVersionUID = 2198223032642500232L;

		@Override
		public int compare(GrantedAuthority g1, GrantedAuthority g2) {
			// Neither should ever be null as each entry is checked before
			// adding it to the set.
			// If the authority is null, it is a custom authority and should
			// precede others.
			if (g2.getAuthority() == null) {
				return -1;
			}

			if (g1.getAuthority() == null) {
				return 1;
			}

			return g1.getAuthority().compareTo(g2.getAuthority());
		}
	}

}
